/* GMClient.cc -- Cliente GEARMedula
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 *
 * This file is part of GEARMedula.
 * 
 * GEARMedula is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 3 of the
 * License.
 * 
 * GEARMedula is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <sstream>
#include <string>

#include "GMClient.hh"

using namespace std;

GMClient::GMClient()
{}

GMClient::~GMClient()
{}

int
GMClient::Init( const string addr, const int port )
{
    try
    {
	int argc = 0;
	CORBA::ORB_var orb = CORBA::ORB_init(argc, NULL);

	stringstream port_stream;
	port_stream << port;
	
	// Objeto Visao
	string visao_string = "corbaname::";
	if( port == 0 )
	    visao_string += addr + "#Visao.gm";	    
	else
	    visao_string += addr + ":" + port_stream.str() + "#Visao.gm";
	
	CORBA::Object_var obj =
	    orb->string_to_object(
		visao_string.c_str());
	if(CORBA::is_nil(obj)){
	    cerr << "Nill Time reference" << endl;
	    throw 0;
	}
	vs_var_ = Visao::_narrow(obj);
	if( CORBA::is_nil(vs_var_)){
	    cerr << "Argument is not a Time reference" << endl;
	    throw 0;
	}
	
	// Objeto IA
	string ia_string = "corbaname::";
	if( port == 0 )
	    ia_string += addr + "#IA.gm";	    
	else
	    ia_string += addr + ":" + port_stream.str() + "#IA.gm";
	CORBA::Object_var obj_ia =
	    orb->string_to_object(
		ia_string.c_str());
	if(CORBA::is_nil(obj_ia)){
	    cerr << "Nill Time reference" << endl;
	    throw 0;
	}

	ia_var_ = IA::_narrow(obj_ia);
	if( CORBA::is_nil(ia_var_)){
	    cerr << "Argument is not a Time reference" << endl;
	    throw 0;
	}

    }
    catch( const CORBA::Exception &)
    {
	cerr << "Uncaught CORBA exception" << endl;
	return 1;
    }
    catch(...)
    {
	return 1;
    }
    return 0;
}

void
GMClient::setField( const vector<float> field )
{
    vs_var_->setField( &field[0] );
}

void
GMClient::setWorld( const vector<float> world )
{
    vs_var_->setWorld( &world[0] );
}

void
GMClient::getField( std::vector<float> &field )
{
    field.resize( LENGTH_FIELD_VECTOR );
    ia_var_->getField( &field[0] );
}

void
GMClient::getWorld( std::vector<float> &world )
{
    world.resize( LENGTH_WORLD_VECTOR );
    ia_var_->getWorld( &world[0] );
}

void
GMClient::SetVel( const int id, const float vx, const float vy, const float va )
{
    ia_var_->SetVel( id, vx, vy, va );
}

void
GMClient::Kick( const int id, const int nivel )
{
    ia_var_->Kick(id, nivel);
}

void
GMClient::DribbleEnable(const int id, const bool enable )
{
    ia_var_->DribbleEnable(id, enable);
}

void
GMClient::SayHello(const int id)
{
    ia_var_->SayHello(id);
}
